cmake_minimum_required(VERSION 3.8)

project(insnet CXX)

if(CMAKE_BUILD_TYPE MATCHES debug)
    add_definitions(-DN3LDG_DEBUG)
    message("building n3ldg++ for debug")
    set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -msse3 -std=c++14 -O1 -fno-inline -ggdb3" )
elseif(CMAKE_BUILD_TYPE MATCHES profile)
    message("building n3ldg++ for profile")
    set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++14 -O2 -pg -DNDEBUG" )
else()
    message("building n3ldg++ for release")
    set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -Ofast -march=native" )
endif()

IF(USE_DOUBLE)
    ADD_DEFINITIONS(-DUSE_DOUBLE)
ENDIF()
IF(TEST_CUDA)
    ADD_DEFINITIONS(-DTEST_CUDA)
ENDIF()
IF(DEVICE_MEMORY)
    ADD_DEFINITIONS(-DDEVICE_MEMORY)
ENDIF()

include_directories(include)
include_directories(include/fmt/include)

if(USE_CUDA)
    set(CUDA_SEPARABLE_COMPILATION ON)
    list(APPEND CUDA_NVCC_FLAGS "--use_fast_math;-arch=compute_${CARD_NUMBER};-code=sm_${CARD_NUMBER};-std=c++14;-DVERBOSE;-DEIGEN_USE_GPU;-DHAVE_CUDA;")
    set(CUDA_PROPAGATE_HOST_FLAGS OFF)
    if (CMAKE_BUILD_TYPE MATCHES debug)
        list(APPEND CUDA_NVCC_FLAGS "-G")
        list(APPEND CUDA_NVCC_FLAGS "-g")
        list(APPEND CUDA_NVCC_FLAGS "-O1")
    else()
        list(APPEND CUDA_NVCC_FLAGS "-O3")
    endif()
    add_definitions(-DUSE_GPU)
    find_package(CUDA REQUIRED)
    find_cuda_helper_libs(curand)
    set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_ROOT})
    message("CUDA_INCLUDE: " ${CUDA_INCLUDE_DIRS})
    include_directories(SYSTEM ${CUDA_INCLUDE_DIRS})
    set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_curand_LIBRARY})
    message("CUDA_LIBRARIES: ${CUDA_LIBRARIES}")
    set(libs ${libs} ${CUDA_LIBRARIES})
    cuda_add_library(insnet_cuda include/insnet/cuda/impl.cu)
    cuda_add_cublas_to_target(insnet_cuda)
    target_compile_features(insnet_cuda PUBLIC cxx_std_14)
    set_target_properties(insnet_cuda PROPERTIES POSITION_INDEPENDENT_CODE ON)
    set(libs ${libs} insnet_cuda)
endif()

add_subdirectory(include/fmt)
aux_source_directory(include/insnet/base insnet_src)
aux_source_directory(include/insnet/block insnet_src)
aux_source_directory(include/insnet/computation-graph insnet_src)
aux_source_directory(include/insnet/loss insnet_src)
aux_source_directory(include/insnet/nlp insnet_src)
aux_source_directory(include/insnet/operator insnet_src)
aux_source_directory(include/insnet/optimizer insnet_src)
aux_source_directory(include/insnet/param insnet_src)
aux_source_directory(include/insnet/util insnet_src)

add_library(insnet STATIC ${insnet_src})
target_include_directories(insnet PUBLIC include PUBLIC include/fmt/include)
set(libs ${libs} fmt)
target_link_libraries(insnet ${libs})
